package com.sxhalo.fsms.controller.api;

import javax.servlet.http.HttpServletRequest;

import com.sxhalo.fsms.controller.BaseController;
import com.sxhalo.fsms.domain.SysRoles;
import com.sxhalo.fsms.log.SysControllerLog;
import com.sxhalo.fsms.service.ISysRoleModulesService;
import com.sxhalo.fsms.service.ISysRolesService;
import com.sxhalo.fsms.util.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/api")
public class SysRoleController extends BaseController {

	@Autowired
	private ISysRolesService roleService;
	@Autowired
	private ISysRoleModulesService rolemoduleService;


	@ResponseBody
	@RequestMapping(value = "/role", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	public ApiResult findRole(Integer current, Integer pageSize, HttpServletRequest req) {
		String where = "1=1";
		String order = "";
		if (!isBlank(req.getParameter("roleName"))) {
			where += " and role_name like '%" + req.getParameter("roleName").trim() + "%'";
		}
		if (!isBlank(req.getParameter("roleDescription"))) {
			where += " and role_description like '%" + req.getParameter("roleDescription").trim() + "%'";
		}

		if (!isBlank(req.getParameter("roleIsdisable"))) {
			where += " and role_isdisable = " + req.getParameter("roleIsdisable").trim() ;
		}

		if (!isBlank(req.getParameter("showSysRole"))) {
			where += " and role_id !=1" ;
		}

		return findList(current, pageSize, where, order, roleService);
	}

	@ResponseBody
	@SysControllerLog(operationName = "新增角色", modelName = "角色管理")
	@RequestMapping(value = "/role", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public ApiResult saveRole(@RequestBody SysRoles record, HttpServletRequest req) {
		if (record != null) {
			record.setRoleId(roleService.getMaxId()+1);
			roleService.insertSelective(record);
		}
		return ApiResult.retOK(req);
	}

	@ResponseBody
	@SysControllerLog(operationName = "更新角色", modelName = "角色管理")
	@RequestMapping(value = "/role", method = RequestMethod.PUT, produces = "application/json;charset=UTF-8")
	public ApiResult updateRole(@RequestBody SysRoles record, HttpServletRequest req) {
		if (record != null) {
			roleService.updateByPrimaryKeySelective(record);
		}
		return ApiResult.retOK(req);
	}

	@SysControllerLog(operationName = "删除", modelName = "模块管理")
	@RequestMapping(value = "/role", method = RequestMethod.DELETE, produces="application/json;charset=UTF-8")
	public ApiResult delModule(Integer roleId, HttpServletRequest req) {
		roleService.deleteByPrimaryKey(roleId);
		rolemoduleService.deleteByRoleId(roleId);
		return ApiResult.retOK(req);
	}


	@ResponseBody
	@SysControllerLog(operationName = "批量删除", modelName = "字典管理")
	@RequestMapping(value = "/role/batchdel", method = RequestMethod.DELETE, produces="application/json;charset=UTF-8")
	public ApiResult batchDelModule(String ids, HttpServletRequest req) {
		roleService.deleteByWhere(" role_id in ("+ids+")");
		rolemoduleService.deleteByWhere(" role_id in ("+ids+")");
		return ApiResult.retOK(req);
	}

}
